package conson.tech.manage.facade.controller.activity.mobile;

import cn.hutool.json.JSONUtil;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.activity.entity.dto.BathBeachTicketDTO;
import conson.tech.manage.activity.entity.dto.BathBeachTicketListDTO;
import conson.tech.manage.activity.entity.pojo.TicketInfo;
import conson.tech.manage.activity.entity.pojo.TicketInfoRecord;
import conson.tech.manage.activity.service.TicketRecordService;
import conson.tech.manage.activity.service.TicketService;
import conson.tech.manage.api.activity.dto.TicketInfoDTO;
import conson.tech.manage.api.activity.dto.TicketInfoRecordDTO;
import conson.tech.manage.api.activity.form.ActivationTicketForm;
import conson.tech.manage.api.activity.form.ReceiveTicketForm;
import conson.tech.manage.api.activity.form.TicketQueryForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName: TicketController
 * Description:
 * Author: HXL
 * Date: 2020/5/22 10:40 上午
 * Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping(SecurityConstants.INNERAPI)
public class TicketController {

    @Autowired
    private TicketService ticketService;
    @Autowired
    private TicketRecordService ticketRecordService;

    @PostMapping("/queryTicket")
    public Response<List<TicketInfoDTO> > queryTicketInfo(@RequestBody TicketQueryForm form){
        log.info("查询票券信息列表入参:[{}]", JSONUtil.toJsonStr(form));
        try {
            List<TicketInfo> infoList= ticketService.queryTicketInfo(form.getUserId());
            List<TicketInfoDTO> infoDTOList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(infoList)){
                infoList.stream().forEach(info->{
                    TicketInfoDTO dto = new TicketInfoDTO();
                    BeanUtils.copyProperties(info,dto );
                    infoDTOList.add(dto);
                });
            }
            return Response.success(infoDTOList);
        } catch (Exception e) {
            log.error("查询票券信息列表,出现异常:",e);
           return Response.fail("系统繁忙,请稍后再试");
        }
    }
    @PostMapping("/queryTicketById")
    public Response<TicketInfoDTO > queryTicketInfoById(@RequestBody TicketQueryForm form){
        log.info("查询票券信息详情入参:[{}]", JSONUtil.toJsonStr(form));
        try {
            TicketInfo info= ticketService.queryTicketInfoById(form.getTicketId(),form.getUserId());
            if (info != null ){
                TicketInfoDTO dto = new TicketInfoDTO();
                BeanUtils.copyProperties(info,dto );
                return  Response.success(dto);
            }
            return Response.success();
        } catch (BeansException e) {
            log.error("查询票券信息详情,出现异常:",e);
            return Response.fail("系统繁忙,请稍后再试");
        }
    }
    @PostMapping("/receiveTicket")
    public Response receiveTicketInfo(@RequestBody ReceiveTicketForm form){
        log.info("领取票券入参:[{}]", JSONUtil.toJsonStr(form));
        try {
            List<TicketInfo> list =  ticketService.queryTicketInfoListById(form.getTicketRecordList());
            List<TicketInfoRecord> recordList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(list)){
                for (TicketInfo ticketInfo : list) {
                    TicketInfoRecord record = new TicketInfoRecord();
                    BeanUtils.copyProperties(ticketInfo, record);
                    record.setId(null);
                    record.setTicketId(String.valueOf(ticketInfo.getId()));
                    recordList.add(record);
                }
            }
            int i = ticketRecordService.receiveTicketInfo(form.getUserId(),form.getUserMobile(),recordList);
            return Response.success(i);
        } catch (Exception e) {
            log.error("领取票券,出现异常:",e);
            return Response.fail("系统繁忙,请稍后再试");
        }
    }

    @PostMapping("/activationTicket")
    public Response<Integer> activationTicketInfo(@RequestBody ActivationTicketForm form){
        log.info("【激活海水浴场票券】入参:[{}]", JSONUtil.toJsonStr(form));
        try {
            TicketInfoRecord record = new TicketInfoRecord();
            record.setId(form.getTicketRecordId());
            TicketInfoRecord record1 = ticketRecordService.checkTermOfValidity(record);
            if (record1 == null){
                return Response.success(2);//表示没有这个票，或者已经过期了
            }
            if (!StringUtils.isEmpty(record1.getUserMobile())){
                if (!record1.getUserMobile().trim().equals(form.getPhone().trim())){
                    log.info("【激活海水浴场票券】- 入参手机号:[{}]与记录手机号:[{}]不匹配", form.getPhone(),record1.getUserMobile());
                    return  Response.success(4);//表示不是一个手机号，不能进行激活操作
                }
            }
            int count = ticketRecordService.checkActivationTicket(form.getPhone());
            if (count >0){
                return Response.success(0);//表示当天已经存在已经激活的票了
            }
            BathBeachTicketDTO dto = new BathBeachTicketDTO();
            BeanUtils.copyProperties(form, dto);
            BathBeachTicketListDTO listDTO = new BathBeachTicketListDTO();
            listDTO.setFaceUrl(form.getFaceUrl());
            listDTO.setPhone(form.getPhone());
            List<BathBeachTicketListDTO> listDTOList = new ArrayList<>();
            listDTOList.add(listDTO);
            dto.setCreateTicketList(listDTOList);
            int i = ticketRecordService.activationTicketInfo(dto);
            if (i>0) {
                return Response.success(1);
            }
            return Response.success(3);
        } catch (BeansException e) {
            log.error("【激活海水浴场票券】,出现异常:",e);
            return Response.fail("系统繁忙,请稍后再试");
        }
    }
    @PostMapping("/queryTicketRecord")
    public Response<List<TicketInfoRecordDTO> > queryTicketInfoRecord(@RequestBody TicketQueryForm form){
        log.info("查询票券记录列表入参:[{}]", JSONUtil.toJsonStr(form));
        try {
            TicketInfoRecord record = new TicketInfoRecord();
            record.setTicketStatus(form.getStatus());
            record.setUserId(form.getUserId());
            record.setDelFlag(1);
            List<TicketInfoRecord> infoList= ticketRecordService.queryTicketInfoRecord(record);
            List<TicketInfoRecordDTO> infoDTOList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(infoList)){
                infoList.stream().forEach(info->{
                    TicketInfoRecordDTO dto = new TicketInfoRecordDTO();
                    BeanUtils.copyProperties(info,dto );
                    infoDTOList.add(dto);
                });
            }
            return Response.success(infoDTOList);
        } catch (Exception e) {
            log.error("查询票券记录列表,出现异常:",e);
            return Response.fail("系统繁忙,请稍后再试");
        }
    }

    @PostMapping("/queryTicketRecordById")
    public Response<TicketInfoRecordDTO > queryTicketInfoRecordById(@RequestBody Map<String,Long> map) {
        log.info("查询票券记录详情入参:[{}]", JSONUtil.toJsonStr(map));
        try {
            if (map.get("id") == null) {
                return Response.success();
            }
            TicketInfoRecord recordById = ticketRecordService.queryTicketInfoRecordById(map.get("id"));
            if (recordById != null) {
                TicketInfoRecordDTO dto = new TicketInfoRecordDTO();
                BeanUtils.copyProperties(recordById, dto);
                return Response.success(dto);
            }
            return Response.success();
        }catch (Exception e) {
            log.error("查询票券记录详情,出现异常:",e);
            return Response.fail("系统繁忙,请稍后再试");
        }
    }
}
